<?php
session_start();
if (empty($_SESSION['username'])) {
    echo '<script type="text/javascript">window.location.href="../index.php";</script>';
} else {
    $now = time();
    if ($now > $_SESSION['expire']) {
        session_destroy();
        echo '<script type="text/javascript">var r=confirm("Session expire (30 mins)!"); if(r==true){window.location.href="../index.php";}else{window.location.href="index.php";}</script>';
    } else {
        require '../model-db-connection/config.php';
        $config = require '../model-db-connection/qrc_conf.properties.php';
    }
}
?>
<script src="../js/jquery.dataTables.js"></script>
<script src="../js/jqueryui-editable.min.js"></script>
<script src="../js/plugins/datatables/ColReorderWithResize.js"></script>
<script src="../js/plugins/datatables/colvis/dataTables.ColVis.min.js"></script>
<script src="../js/plugins/datatables/tabletools/ZeroClipboard.js"></script>
<script src="../js/plugins/datatables/tabletools/dataTables.TableTools.min.js"></script>
<script src="../js/dataTables.bootstrap.js"></script>

<link rel="stylesheet" href="../css/plugins/dataTables.bootstrap.css">
<link rel="stylesheet" href="../css/ColVis.css">
<link rel="stylesheet" href="../css/jqueryui-editable.css">

<script type="text/javascript" src="../js/zebra_dialog.js"></script>
<link rel="stylesheet" href="../css/zebra_dialog.css" type="text/css">

<table class="table table-striped table-bordered table-hover" id="dataTables-example" width="100%">
    <thead>
        <tr>

            <th data-class="expand" class="center">INV Code.</th>
            <th class="center">Customer Name</th>
            <th class="center">Create Time</th>
            <th class="center">Status</th>
            <th class="center"></th>

        </tr>
    </thead>
    <tbody>
        <?php
        $ryearThai = date('Y') + 543;
        $sqlSelectAllProjectRecord = "select *"
                . " from QRC_INVOICE qi"
                . " LEFT JOIN QRC_CUSTOMER_NAME qc on qc.customer_id = qi.customer_id"
                . " LEFT JOIN QRC_TYPE_OF_SERVICE qt on qi.order_type = qt.service_id"
                . " LEFT JOIN QRC_INVOICE_STATUS qis on qi.invoice_status = qis.inv_staus_id";
        $sqlGetAllData = mysql_query($sqlSelectAllProjectRecord);
        if (mysql_num_rows($sqlGetAllData) >= 1) {
            while ($row = mysql_fetch_assoc($sqlGetAllData)) {

                echo '<tr class = "gradeX">';

                echo '<td>' . $row['inv_id'] . '</td>';
                echo '<td>' . $row['customer_name'] . '</td>';
                echo '<td>' . $row['create_date_time'] . '</td>';
                $prefix = 'QRC' . substr($ryearThai, -2) . '-INV';
                if (substr($row['inv_id'], 0, -7) == $prefix) {
                    echo '<td><a href="#" title="Edit status" data-type="select" data-pk="' . $row['inv_id'] . '" data-value="' . $row['inv_staus_id'] . '" id="' . $row['inv_id'] . '" onclick=editTable("' . $row['inv_id'] . '")>' . $row['inv_staus_name'] . '</a></td>';
                } else {
                    echo '<td></td>';
                }
                echo '<td align="center">';
                echo '<div class = "btn-group margin-bottom-20">';
                echo '<button type = "button" class = "btn btn-default dropdown-toggle btn-xs" data-toggle = "dropdown">Actions <span class = "caret"></span></button>';
                echo '<ul class = "dropdown-menu" role = "menu">';
                echo '<li><a href = "#" class="btn-xs" onclick=generateBilling("' . $row['inv_id'] . '",' . $row['customer_id'] . ',"Copy")><i class = "fa fa-download"></i> Download Invoice Copy</a></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=generateBilling("' . $row['inv_id'] . '",' . $row['customer_id'] . ',"Original")><i class = "fa fa-download"></i> Download Invoice Original</a></li>';
                echo '<li class = "divider"></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=generateBilling("' . $row['create_receipt'] . '",' . $row['customer_id'] . ',"Copy")><i class = "fa fa-download"></i> Download Receipt Copy</a></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=generateBilling("' . $row['create_receipt'] . '",' . $row['customer_id'] . ',"Original")><i class = "fa fa-download"></i> Download Receipt Original</a></li>';
                echo '<li class = "divider"></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=generateProgressive("' . $row['inv_id'] . '",' . $row['customer_id'] . ',"Copy")><i class = "fa fa-download"></i> Download progressive Copy</a></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=generateProgressive("' . $row['inv_id'] . '",' . $row['customer_id'] . ',"Original")><i class = "fa fa-download"></i> Download progressive Original</a></li>';
                echo '<li class = "divider"></li>';
                echo '<li><a href = "#" class="btn-xs" onclick=deleteBilling("' . $row['inv_id'] . '")><i class = "fa fa-trash-o"></i> Delete (ลบ)</a></li>';
                echo '</ul>';
                echo '</div>';
                echo '</td>';
                echo '</tr>';
            }
        }
        ?>                                          
    </tbody>
</table>
<script>
    $(document).ready(function() {
        $.fn.editable.defaults.mode = 'popup';
        var datableConfig = $('#dataTables-example').dataTable({
            "order": [[0, "desc"]]
        });
    });
    function viewClick(po_id) {
        $.Zebra_Dialog({
            source: {'ajax': 'AjaxViewContent.php?po_id=' + po_id},
            width: 600,
            title: 'Project Detail'
        });
    }
    function editTable(inv_id) {
        $("#" + inv_id).editable({
            value: 2,
            source: [
                {value: 44001, text: 'New'},
                {value: 44002, text: 'Open'},
                {value: 44003, text: 'Pending'},
                {value: 44004, text: 'Closed'}
            ],
            url: '../model/com.qrc.mgr.controller/UpdateInvStatus.php'
        });
    }
</script>